package com.yellowpage.bigdata.action;

import java.io.IOException;
import java.util.*;

import com.opensymphony.xwork2.ActionContext;
import com.yellowpage.tool.Excelexport;
import com.yellowpage.white.common.Configuration;
import com.yellowpage.white.common.Constants;
import com.yellowpage.white.struts.BaseAction;
import com.yellowpage.white.struts.Module;
import com.yellowpage.white.struts.OperationType;
import com.yellowpage.white.util.Util4Java;
import com.yellowpage.bigdata.data.NspOnofflogRequsetSum;
import com.yellowpage.bigdata.service.NspFatallogRequsetSumService;
import com.yellowpage.bigdata.service.NspOnofflogRequsetSumService;
import com.yellowpage.ca.data.User;
import com.yellowpage.ca.service.RegionService;

public class NspOnofflogRequsetSumAction extends BaseAction {

	public String RESULT_QUERY = "query";
	public String RESULT_ADD = "add";
	public String RESULT_EDIT = "edit";

	private Configuration configuration;

	public void setConfiguration(Configuration configuration) {
		this.configuration = configuration;
	}

	private List<NspOnofflogRequsetSum> nspOnofflogRequsetSumList = new ArrayList<NspOnofflogRequsetSum>();
	private List<Map> resultList = new ArrayList<Map>();

	private NspOnofflogRequsetSum nspOnofflogRequsetSum;
	private NspOnofflogRequsetSumService nspOnofflogRequsetSumService;

	private String selectedId;
	private String[] selectedIds;
	private String message;

	private String query_logDate;
	private String query_appVersion;
	private String query_operator;
	private String query_channelNo;
	private String query_optType;
	private String query_createUser;
	private String query_createTime;
	private String query_note;
	private String query_onofflogRequsetSumId;
	private String query_requestCount;
	private String query_logDate_end;
	private RegionService regionService;
	private String regionName;
	private Map<String, String> regionMap;
	private NspFatallogRequsetSumService nspFatallogRequsetSumService;

	public NspFatallogRequsetSumService getNspFatallogRequsetSumService() {
		return nspFatallogRequsetSumService;
	}

	public void setNspFatallogRequsetSumService(
			NspFatallogRequsetSumService nspFatallogRequsetSumService) {
		this.nspFatallogRequsetSumService = nspFatallogRequsetSumService;
	}

	public Map<String, String> getRegionMap() {
		return regionMap;
	}

	public void setRegionMap(Map<String, String> regionMap) {
		this.regionMap = regionMap;
	}

	public String getRegionName() {
		return regionName;
	}

	public void setRegionName(String regionName) {
		this.regionName = regionName;
	}

	public RegionService getRegionService() {
		return regionService;
	}

	public void setRegionService(RegionService regionService) {
		this.regionService = regionService;
	}

	public String getRegionSql() {
		Map session = ActionContext.getContext().getSession();
		User user = (User) session.get("userObject");
		String function_code = "OMC_CLIENT_LOG_COUNT_REQUEST_INTERFACE";
		String regionsql = regionService.getRegionclauseByUserFunction(
				user.getUserId(), function_code, "REGION_CODE");
		return regionsql;
	}
	public String queryChannel() {
		initPageRender();
		regionMap = nspFatallogRequsetSumService.getRegionMap(getRegionSql());
		setPageRenderToRequest();
		return "toRegion";
	}
	public void prepare() {
		super.prepare();

		if (this.selectedId != null && !this.selectedId.equals("")) {
			this.nspOnofflogRequsetSum = nspOnofflogRequsetSumService
					.getNspOnofflogRequsetSum(Long.parseLong(selectedId));
		}
	}

	public String queryByPage() {
		initPageRender();

		Map queryMap = new HashMap();
		queryMap.put("logDate", query_logDate);
		queryMap.put("query_logDate_end", query_logDate_end);
		if(!regionName.equals("")){
			regionMap = nspFatallogRequsetSumService.getRegionMap(getRegionSql());
			String[] val = regionName.split(";");
			String str = "(";
			for(String ss : val){
				if("A".equals(ss)){
					for(String key : regionMap.keySet()){
						if(key.equals("000000")){
    						str += "'" + ss + "',";
    						break;
    					}
					}
					continue;
				}
				for(String key : regionMap.keySet()){
					if(regionMap.get(key).contains(ss)){
						str += "'" + key + "',";
						break;
					}
				}
			}
			str= str.substring(0, str.length() - 1)+")";
			if(")".equals(str)){
				return RESULT_QUERY;
			}
			queryMap.put("query_regionCode", str);
		}else{
			queryMap.put("query_regionCode", regionName);
		}
		queryMap.put("query_appVersion", query_appVersion);
		queryMap.put("query_operator", query_operator);
		queryMap.put("query_channelNo", query_channelNo);
		queryMap.put("query_optType", query_optType);

		resultList = nspOnofflogRequsetSumService
				.queryNspOnofflogRequsetSumByPage(queryMap, pageRender);

		setPageRenderToRequest();

		return RESULT_QUERY;
	}

	//给出excel导出策略
	public void exportExcel()

	{
		Map queryMap = new HashMap();
		queryMap.put("logDate", query_logDate);
		queryMap.put("query_logDate_end", query_logDate_end);
		
		if(!regionName.equals("")){
			regionMap = nspFatallogRequsetSumService.getRegionMap(getRegionSql());
			String[] val = regionName.split(";");
			String str = "(";
			for(String ss : val){
				if("A".equals(ss)){
					for(String key : regionMap.keySet()){
						if(key.equals("000000")){
    						str += "'" + ss + "',";
    						break;
    					}
					}
					continue;
				}
				for(String key : regionMap.keySet()){
					if(regionMap.get(key).contains(ss)){
						str += "'" + key + "',";
						break;
					}
				}
			}
			str= str.substring(0, str.length() - 1)+")";
			if(")".equals(str)){
				resultList = null;
			}
			queryMap.put("query_regionCode", str);
		}else{
			queryMap.put("query_regionCode", regionName);
		}
		queryMap.put("query_appVersion", query_appVersion);
		queryMap.put("query_operator", query_operator);
		queryMap.put("query_channelNo", query_channelNo);
		queryMap.put("query_optType", query_optType);

		if(resultList != null){
			
			resultList = nspOnofflogRequsetSumService
					.queryNspOnofflogRequsetSumByPage(queryMap, null);
		}

		LinkedHashMap<String,String> namemap= new LinkedHashMap<String,String>();



		//给出中文名称
		namemap.put("LOG_DATE","日期");
		namemap.put("REGION_CODE","地区代码");
		namemap.put("REGION_NAME","地区名称");
		namemap.put("APP_VERSION","版本");
		namemap.put("OPERATOR","运营商");
		namemap.put("CHANNEL_NO","渠道号");
		namemap.put("CHANNEL_NO_NAME","渠道名称");
		namemap.put("OPT_TYPE","操作类型");
		namemap.put("REQUEST_COUNT","请求数");

		Excelexport.exportExcel(resultList, this.getClass().getName(), namemap);


	}


	public String toAdd() {
		return RESULT_ADD;
	}

	public String save() {

		nspOnofflogRequsetSumService
				.saveNspOnofflogRequsetSum(nspOnofflogRequsetSum);

		message = "新增成功!";
		return RESULT_ADD;
	}

	public void validateSave() {
		if (nspOnofflogRequsetSum.getLogDate().length() == 0) {
			this.addActionError("** LogDate 不能为空 !");
		}
		if (nspOnofflogRequsetSum.getRegionCode().length() == 0) {
			this.addActionError("** RegionCode 不能为空 !");
		}
		if (nspOnofflogRequsetSum.getAppVersion().length() == 0) {
			this.addActionError("** AppVersion 不能为空 !");
		}
		if (nspOnofflogRequsetSum.getOperator().length() == 0) {
			this.addActionError("** Operator 不能为空 !");
		}
		if (nspOnofflogRequsetSum.getChannelNo().length() == 0) {
			this.addActionError("** ChannelNo 不能为空 !");
		}
		if (nspOnofflogRequsetSum.getOptType().length() == 0) {
			this.addActionError("** OptType 不能为空 !");
		}
		if (nspOnofflogRequsetSum.getNote().length() == 0) {
			this.addActionError("** Note 不能为空 !");
		}

		this.setValidatorResult("/bigdata/NspOnofflogRequsetSumAdd.jsp");
	}

	public String toEdit() {
		Long id = new Long(selectedId);

		nspOnofflogRequsetSum = nspOnofflogRequsetSumService
				.getNspOnofflogRequsetSum(id);

		return RESULT_EDIT;
	}

	public String update() {

		nspOnofflogRequsetSumService
				.updateNspOnofflogRequsetSum(nspOnofflogRequsetSum);

		message = "修改成功!";

		return RESULT_EDIT;
	}

	public void validateUpdate() {
		if (nspOnofflogRequsetSum.getLogDate().length() == 0) {
			this.addActionError("** LogDate 不能为空 !");
		}
		if (nspOnofflogRequsetSum.getRegionCode().length() == 0) {
			this.addActionError("** RegionCode 不能为空 !");
		}
		if (nspOnofflogRequsetSum.getAppVersion().length() == 0) {
			this.addActionError("** AppVersion 不能为空 !");
		}
		if (nspOnofflogRequsetSum.getOperator().length() == 0) {
			this.addActionError("** Operator 不能为空 !");
		}
		if (nspOnofflogRequsetSum.getChannelNo().length() == 0) {
			this.addActionError("** ChannelNo 不能为空 !");
		}
		if (nspOnofflogRequsetSum.getOptType().length() == 0) {
			this.addActionError("** OptType 不能为空 !");
		}
		if (nspOnofflogRequsetSum.getNote().length() == 0) {
			this.addActionError("** Note 不能为空 !");
		}
		this.setValidatorResult("/bigdata/NspOnofflogRequsetSumUpdate.jsp");
	}

	public String delete() {
		List ids = new ArrayList();

		for (String id : selectedIds) {
			ids.add(Long.valueOf(id));
		}

		try {
			this.nspOnofflogRequsetSumService.delete(ids);
		} catch (Exception e) {
			System.out.println("异常....");
			this.addActionError("** 可能含有子记录约束冲突删除失败 !");
		}

		return queryByPage();
	}

	/**
	 * getters and setters
	 * 
	 * @return
	 */
	public void setNspOnofflogRequsetSum(
			NspOnofflogRequsetSum nspOnofflogRequsetSum) {
		this.nspOnofflogRequsetSum = nspOnofflogRequsetSum;
	}

	public NspOnofflogRequsetSum getNspOnofflogRequsetSum() {
		return nspOnofflogRequsetSum;
	}

	public void setNspOnofflogRequsetSumService(
			NspOnofflogRequsetSumService nspOnofflogRequsetSumService) {
		this.nspOnofflogRequsetSumService = nspOnofflogRequsetSumService;
	}

	public List<NspOnofflogRequsetSum> getNspOnofflogRequsetSumList() {
		return nspOnofflogRequsetSumList;
	}

	public void setNspOnofflogRequsetSumList(
			List<NspOnofflogRequsetSum> nspOnofflogRequsetSumList) {
		this.nspOnofflogRequsetSumList = nspOnofflogRequsetSumList;
	}

	public String getSelectedId() {
		return selectedId;
	}

	public void setSelectedId(String selectedId) {
		this.selectedId = selectedId;
	}

	public String[] getSelectedIds() {
		return selectedIds;
	}

	public void setSelectedIds(String[] selectedIds) {
		this.selectedIds = selectedIds;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public List<Map> getResultList() {
		return resultList;
	}

	public void setResultList(List<Map> resultList) {
		this.resultList = resultList;
	}

	public String getQuery_logDate() {
		return this.query_logDate;
	}

	public void setQuery_logDate(String logDate) {
		this.query_logDate = logDate;
	}

	public String getQuery_appVersion() {
		return this.query_appVersion;
	}

	public void setQuery_appVersion(String appVersion) {
		this.query_appVersion = appVersion;
	}

	public String getQuery_operator() {
		return this.query_operator;
	}

	public void setQuery_operator(String operator) {
		this.query_operator = operator;
	}

	public String getQuery_channelNo() {
		return this.query_channelNo;
	}

	public void setQuery_channelNo(String channelNo) {
		this.query_channelNo = channelNo;
	}

	public String getQuery_optType() {
		return this.query_optType;
	}

	public void setQuery_optType(String optType) {
		this.query_optType = optType;
	}

	public String getQuery_createUser() {
		return this.query_createUser;
	}

	public void setQuery_createUser(String createUser) {
		this.query_createUser = createUser;
	}

	public String getQuery_createTime() {
		return this.query_createTime;
	}

	public void setQuery_createTime(String createTime) {
		this.query_createTime = createTime;
	}

	public String getQuery_note() {
		return this.query_note;
	}

	public void setQuery_note(String note) {
		this.query_note = note;
	}

	public String getQuery_onofflogRequsetSumId() {
		return this.query_onofflogRequsetSumId;
	}

	public void setQuery_onofflogRequsetSumId(String onofflogRequsetSumId) {
		this.query_onofflogRequsetSumId = onofflogRequsetSumId;
	}

	public String getQuery_requestCount() {
		return this.query_requestCount;
	}

	public void setQuery_requestCount(String requestCount) {
		this.query_requestCount = requestCount;
	}

	public String getQuery_logDate_end() {
		return query_logDate_end;
	}

	public void setQuery_logDate_end(String query_logDate_end) {
		this.query_logDate_end = query_logDate_end;
	}

}
